import { DD_SYSTEM } from '@/constants/api-pre';
import { request } from '@/service/request';

/**
 * user gender
 *
 * - "1": "male"
 * - "2": "female"
 * - "3": "保密"
 */
export type UserGender = '1' | '2' | '3';

/** user */
export type User = Api.Common.CommonRecord<{
  /** user name */
  username: string;
  /** user gender */
  gender: UserGender | null | string;
  /** user nick name */
  nickname: string;
  /** user phone */
  mobile: string;
  /** user email */
  email: string;
  /** user role code collection */
  roles: string[];
  /** 备注 */
  remark: string;
  /** 用户编号 */
  userId: number;
  /** 部门编号 */
  deptId: number;
  /** 部门名称 */
  deptName: string;
  /** 用户岗位 */
  postIds: number[];
}>;

/** user search params */
export type UserSearchParams = CommonType.RecordNullable<
  Pick<User, 'username' | 'gender' | 'nickname' | 'mobile' | 'email' | 'status' | 'userId' | 'deptId'> &
    Api.Common.CommonSearchParams
>;

export type UserEditAndCreateParams = CommonType.RecordNullable<
  Pick<User, 'username' | 'gender' | 'nickname' | 'mobile' | 'email' | 'status'>
>;

/** user list */
export type UserList = Api.Common.PaginatingQueryRecord<User>;

/** 获取用户表单 URL */
export function getFetchUserUrl(id: number) {
  return `${DD_SYSTEM}/users/${id}/form`;
}

/** get user list */
export function fetchGetUserList(params?: UserSearchParams) {
  return request<UserList>({
    url: `${DD_SYSTEM}/users/page`,
    method: 'get',
    params
  });
}

/**
 * 通过id获取用户信息
 *
 * @param id
 * @returns
 */
export function fetchGetUserById(id: number) {
  return request<User>({
    url: getFetchUserUrl(id),
    method: 'get'
  });
}

/**
 * 获取用户下拉选项-带分页
 *
 * @returns
 */
export function fetchUserOptionsPage(params?: UserSearchParams) {
  return request<Api.Common.OptionsPage>({
    url: `${DD_SYSTEM}/users/options`,
    method: 'get',
    params
  });
}

/**
 * 编辑用户
 *
 * @param id
 * @param data
 */
export function updateUser(id: number, data?: UserEditAndCreateParams) {
  return request<boolean>({
    url: `${DD_SYSTEM}/users/${id}`,
    method: 'put',
    data
  });
}

/**
 * 编辑用户状态
 *
 * @param id
 */
export function updateUserStatus(id: number) {
  return request<boolean>({
    url: `${DD_SYSTEM}/users/${id}/status`,
    method: 'patch'
  });
}

/**
 * 重置用户密码
 *
 * @param id
 */
export function updateUserPassword(id: number, password: string) {
  return request<boolean>({
    url: `${DD_SYSTEM}/users/${id}/password`,
    method: 'patch',
    data: {
      password
    }
  });
}

/**
 * 新增用户
 *
 * @param data
 */
export function createUser(data: UserEditAndCreateParams) {
  return request<boolean>({
    url: `${DD_SYSTEM}/users`,
    method: 'post',
    data
  });
}

/**
 * 删除用户
 *
 * @param ids
 */
export function deleteUsers(ids: string) {
  return request<boolean>({
    url: `${DD_SYSTEM}/users/${ids}`,
    method: 'delete'
  });
}

/**
 * 设置用户的角色
 *
 * @param userId
 * @param roleIds
 */
export function assignUserRole(userId: number, roleIds: string[]) {
  return request<boolean>({
    url: `${DD_SYSTEM}/permission/user/${userId}/roles`,
    method: 'post',
    data: roleIds
  });
}
